package com.andnnl.videosms;

import com.alibaba.fastjson.JSON;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Created by chenss on 2022/2/15.
 */
public class VideoSmsParamValidateTest {
    public static void main(String[] args) {
        String msg="";

        msg="6212";
        System.out.println("rs:"+validate(msg));
        System.out.println();

        msg="6212?1-2:a=a11&b=123123|1-2:a=a11&b=苛夺苛夺苛夺工顶戴顶戴塔顶地";
        System.out.println("rs:"+validate(msg));
        System.out.println();

        msg="6212?1-2:a=a11&b=123123|1-2:a=a11&b=苛夺苛夺苛夺工顶&戴33、苛夺--顶戴塔顶地";
        System.out.println("rs:"+validate(msg));
        System.out.println(msg.getBytes().length);

        msg="{\"t\":\"12232\",[{\"fo\":\"1-2\",\"p\":{\"a\":\"a11\",\"b\":\"123123\"}},{\"fo\":\"1-3\",\"p\":{\"a\":\"a11\",\"b\":\"苛夺苛夺苛夺工顶戴顶戴塔顶地\"}}]}";
        System.out.println(msg.getBytes().length);

    }

    private static boolean validate(String msg) {
        msg = msg.trim();
        //校验模板是否存在
        int tempId = 0;
        String content = "";
        try {
            //解析内容，区分动参
            String[] ms = msg.split("\\?", 2);
            tempId = Integer.parseInt(ms[0]);
            if (ms.length == 2) {
                String ff = ms[1];//动参
                String[] frameStr = ff.split("\\|");
                List<Param> list = new ArrayList<>();
                for (String fs : frameStr) {
                    String[] fps = fs.split(":");
                    String[] foStr = fps[0].split("-", 2);
                    String frame = foStr[0];
                    String order = foStr[1];
                    String[] ps = fps[1].split("&");
                    Param param = new Param();
                    param.setFrame(Integer.parseInt(frame));
                    param.setOrder(Integer.parseInt(order));
                    HashMap paramMap = new HashMap();
                    param.setParam(paramMap);
                    for (String p : ps) {
                        String[] paramStr = p.split("=", 2);
                        String name = paramStr[0];
                        String value = paramStr[1];
                        paramMap.put(name, value);
                    }
                    list.add(param);
                }
                content = JSON.toJSONString(list);
                System.out.println(content);
            }
            System.out.println("tempId:"+tempId);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("解析参数错误:"+e.getMessage());
        }
        return false;
    }

    public static class Param{
        private int frame; //帧
        private int order; //帧内序列
        private HashMap param; //参数值

        public Param(){

        }

        public int getFrame() {
            return frame;
        }

        public void setFrame(int frame) {
            this.frame = frame;
        }

        public int getOrder() {
            return order;
        }

        public void setOrder(int order) {
            this.order = order;
        }

        public HashMap getParam() {
            return param;
        }

        public void setParam(HashMap param) {
            this.param = param;
        }
    }

}
